﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Windows.Forms;
using Cashier;
using NomClasses;
using System.Collections;

namespace NomCafe
{
    public partial class frmAddItem : DevComponents.DotNetBar.Office2007RibbonForm
    {
        int indexItem; 
        int indexSelectItem;
        string nameItems;

        nom_bill_detail billDetail = new nom_bill_detail();
        NomDataContext nc = new NomDataContext();

        // bill của bàn đang chọn
        private nom_bill bill;
        
        public frmAddItem()
        {
            InitializeComponent();
        }        

        private void frmAddItem_Load(object sender, EventArgs e)
        {
            panelEx1.Text = "Quản Lý Danh Sách Món : " + Lib_Cashier.currentNodeName + "(" + Lib_Cashier.currentRootName + ")";
            initDatabase();
        }

        #region Content item for the Listview name item

        private void clearText()
        {     
            inputNote.Text = "";
            inputNumber.Text = null;
            listViewNameItem.Items.Clear();
        }

        private void initDatabase()
        {
            // Lấy name item category cho cbbTypeItem
            ArrayList ItemCategory = new ArrayList();
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_item_category> Category = nc.nom_item_categories;
            foreach (NomClasses.nom_item_category itemCate in Category)
            {
                cbbTypeItem.Items.Add(new Item(itemCate.name, "", itemCate.id));                
            }
            getDataItems();
        }

        // Lấy thông tin món đã có trên bàn 
        private void getDataItems()
        {
            NomDataContext nc = new NomDataContext();
            // Lấy bill_id của bàn đang chọn
            bill = nc.nom_bills.Single(b => b.table_id == Main_Cashier.currenTable.id && b.time_end == null);

            var billDetail = from bill_de in nc.nom_bill_details
                                         where bill_de.bill_id == bill.id
                                         select bill_de;
            foreach (var itemOnBill in billDetail)
            {
                string[] row = {itemOnBill.quantity.ToString(), // số lượng               
                                itemOnBill.specifications, // quy cách
                                String.Format("{0:0,0}",itemOnBill.price), // đơn giá 
                                String.Format("{0:0,0}",itemOnBill.cash),// thành tiền
                                itemOnBill.note};
                listViewTableSelectItem.Items.Add(itemOnBill.nom_item.name).SubItems.AddRange(row);
            }
        }

        private class Item
        {
            public string Name;
            public string Mode;
            public int Value;
            public Item(string name, string mode,int value)
            {
                Name = name; Value = value; Mode = mode;
            }
            public override string ToString()
            {
                // Generates the text shown in the combo box
                return Name;
            }
        }

        #endregion
       
        private void btnAddItem_Click(object sender, EventArgs e)
        {
            bool flag = false;
            int itemId = 0;
            if (inputNumber.Text != "")
            {
                Decimal thanhtien;
                for (int i = 0; i < listViewNameItem.Items.Count; i++)
                {
                    if (listViewNameItem.Items[i].Index == indexSelectItem)
                    {                        
                        var billDetail = from detail in nc.nom_bill_details
                                         where detail.bill_id == bill.id
                                         select detail;
                        // Kiểm tra món đã có trên bàn hay chưa
                        foreach (var detail in billDetail)
                        {
                            if (detail.item_id == Convert.ToInt16(listViewNameItem.Items[i].SubItems[3].Text))
                            {
                                flag = true;
                                itemId = detail.item_id;                                
                            }                            
                        }                        
                        if (flag == true)
                        {
                            // Sửa lại số lượng và thành tiền nếu món đã có trên bàn
                            nom_bill_detail details = nc.nom_bill_details.Single(d => d.item_id == itemId && d.bill_id == bill.id); 

                            thanhtien = Decimal.Parse(listViewNameItem.Items[i].SubItems[1].Text) * (details.quantity + inputNumber.Value);
                            details.quantity = Convert.ToByte(details.quantity + inputNumber.Value);
                            details.cash = thanhtien;
                                                        
                            nc.SubmitChanges();
                        }
                        else
                        {
                            thanhtien = Decimal.Parse(listViewNameItem.Items[i].SubItems[1].Text) * inputNumber.Value;

                            nom_bill_detail details = new nom_bill_detail();
                            details.bill_id = bill.id;
                            details.item_id = Convert.ToInt16(listViewNameItem.Items[i].SubItems[3].Text);
                            details.quantity = Convert.ToByte(inputNumber.Text);
                            details.price = Decimal.Parse(listViewNameItem.Items[i].SubItems[1].Text);
                            details.specifications = listViewNameItem.Items[i].SubItems[2].Text;
                            details.cash = thanhtien;
                            details.note = inputNote.Text;
                            details.group_table = Main_Cashier.currenTable.code;

                            nc.nom_bill_details.InsertOnSubmit(details);
                            nc.SubmitChanges();
                        }
                        
                    }
                }
                listViewTableSelectItem.Items.Clear();
                getDataItems();
                inputNumber.Enabled = false;
                inputNote.Enabled = false;
                btnAddItem.Enabled = false;
            }
            else
            {
                MessageBox.Show(languageCashier.ADDITEM_ERROR,"Error",MessageBoxButtons.OK , MessageBoxIcon.Error);
                inputNumber.Focus();
            }
        }

        private void listViewTableSelectItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnAddItem.Enabled = false;
            btnDeleteItem.Enabled = true;
            btnEditItem.Enabled = true;
            cbbTypeItem.Enabled = false;
            inputNumber.Enabled = true;
            inputNote.Enabled = true;            
            for (int i = 0; i < listViewTableSelectItem.Items.Count; i++)
            {
                if (listViewTableSelectItem.Items[i].Selected)
                {
                    indexItem           = listViewTableSelectItem.Items[i].Index;
                    nameItems = listViewTableSelectItem.Items[i].SubItems[0].Text;
                }
            }
            billDetail = nc.nom_bill_details.Single(d => d.bill_id == bill.id && d.nom_item.name == nameItems);
            inputNumber.Text = billDetail.quantity.ToString();
            inputNote.Text = billDetail.note;
        }
                  
        private void btnDeleteItem_Click(object sender, EventArgs e)
        {    
            nc.nom_bill_details.DeleteOnSubmit(billDetail);
            nc.SubmitChanges();

            listViewTableSelectItem.Items.Clear();
            getDataItems();
        }

        private void btnEditItem_Click(object sender, EventArgs e)
        {
            billDetail.cash = billDetail.price * inputNumber.Value;
            billDetail.quantity = Convert.ToByte(inputNumber.Value);
            billDetail.note = inputNote.Text;
            
            nc.SubmitChanges();

            listViewTableSelectItem.Items.Clear();
            getDataItems();
        }

        private void cbbTypeItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            clearText();
            
            ArrayList itemArray = new ArrayList();
            Item itm = (Item)cbbTypeItem.SelectedItem;            
                        
            var items = from item in nc.nom_items
                        where item.item_category_id == itm.Value
                        select item;
            foreach (var ite in items)
            {
                string[] row = { ite.price.ToString(), ite.nom_specification.specification, ite.id.ToString() };
                listViewNameItem.Items.Add(ite.name).SubItems.AddRange(row);    
            }            
        }

        private void listViewNameItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            inputNumber.Enabled = true;
            inputNumber.Text = null;
            inputNote.Enabled = true;
            inputNote.Text = "";
            btnAddItem.Enabled = true;
            cbbTypeItem.Enabled = true;
            btnDeleteItem.Enabled = false;
            btnEditItem.Enabled = false;            
            // lấy index của item đã chọn
            for (int i = 0; i < listViewNameItem.Items.Count; i++)
            {
                if (listViewNameItem.Items[i].Selected)
                {
                    indexSelectItem = listViewNameItem.Items[i].Index;                    
                }
            }
            inputNumber.Focus();
        }

        private void inputNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)            
            {
                if (inputNumber.Text == "")
                {
                    MessageBox.Show(languageCashier.ADDITEM_ERROR, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    inputNumber.Focus();
                }
                else
                {
                    btnAddItem_Click(this, e);
                }
            }
                
        }

        private void btnCloseWindows_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
    }
}
